<?php
/**
* Date class file.
*
* @author Federico Ramirez <fedekiller@gmail.com>
* @link http://code.google.com/p/akaikiwi/
* @copyright Copyright &copy; 2008-2011 Federico Ramirez
* @license http://www.opensource.org/licenses/mit-license.php MIT Licence
*/

/*
* This class extends PHP's DateTime to add more options on formats
*/
class Date extends DateTime {
	private $format = 'long';
	public static $FORMATS = array(
		'db'      => 'Y-m-d H:i:s',
		'number'  => 'YmdHis',
		'time'    => 'H:i',
		'short'   => 'd M H:i',
		'long'    => 'F d, Y H:i',
		'atom'    => DateTime::ATOM,
		'cookie'  => DateTime::COOKIE,
		'iso8601' => DateTime::ISO8601,
		'rfc822'  => DateTime::RFC822,
		'rfc850'  => DateTime::RFC850,
		'rfc1036' => DateTime::RFC1036,
		'rfc1123' => DateTime::RFC1123,
		'rfc2822' => DateTime::RFC2822,
		'rfc3339' => DateTime::RFC3339,
		'rss'     => DateTime::RSS,
		'w3c'     => DateTime::W3C
	);

	/**
	 * Set the format format of the date
	 * @param String $format
	 * @return A string with the formatted time
	 */
	public function format($format = NULL) {
		$this->format = $this->get_format($format);
		
		return parent::format(self::get_format($format));
	}

	/**
	 * Gets the current format
	 * @param String $format
	 */
	public function get_format($format = NULL) {
		if($format == NULL) {
			return $this->format;
		}

		if(array_key_exists($format, self::$FORMATS)) {
			return self::$FORMATS[$format];
		}

		return $format;
	}

	public function __toString() {
		return $this->format();
	}
}
